import { createPinia } from "pinia";
const pinia = createPinia();
pinia.use((content) => {
  content.store.$subscribe((a, b) => {
    console.log(b.count);
    // 每当状态发生变化时，将整个 state 持久化到本地存储。
    // localStorage.setItem("cart", JSON.stringify(b));
  });

  content.store.$onAction(({ name, after,onError }) => {
    if (name == "promiseEx") {
      after((result) => {
        console.log(`Finished "${name}" after Result: ${result}.`);
      });
      // 如果 action 抛出或返回一个拒绝的 promise，这将触发
      onError((error) => {
        console.warn(
          `Failed "${name}" after Error: ${error}.`
        );
      });
    }
  });
});

// 使用任意防抖库

pinia.use(({ options, store }) => {
  if (options.debounce) {
    // 我们正在用新的 action 来覆盖这些 action
    return Object.keys(options.debounce).reduce((debouncedActions, action) => {
      debouncedActions[action] = _.debounce(
        store[action],
        options.debounce[action]
      )
      return debouncedActions
    }, {})
  }
})
export default pinia;
